package com.globagames.strategicgames.util
{
	import com.globagames.strategicgames.model.IArmyModule;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import mx.events.ModuleEvent;
	import mx.modules.IModuleInfo;
	import mx.modules.ModuleManager;
	
	public class GameModuleManager extends EventDispatcher
	{
		protected var _moduleInfo:IModuleInfo;
		[Bindable]
		public var moduleLoaded:Boolean = false;
		public var armyModule:IArmyModule;
		public function GameModuleManager()
		{
               
		}
		public function setModule(factionCode:String ) {
			moduleLoaded = false;
			if( factionCode == 'GRIFFINS' ) {
				_moduleInfo = ModuleManager.getModule("modules/armies/GriffinsArmyModule.swf");
			} else if( factionCode == 'WOLFENS' ) {
				_moduleInfo = ModuleManager.getModule("modules/armies/WolfenArmyModule.swf");
			}
			// add some listeners
           _moduleInfo.addEventListener(ModuleEvent.READY, onModuleReady);
           _moduleInfo.addEventListener(ModuleEvent.SETUP, onModuleSetup);
           _moduleInfo.addEventListener(ModuleEvent.UNLOAD, onModuleUnload);
           _moduleInfo.addEventListener(ModuleEvent.PROGRESS, onModuleProgress);
		}
		protected function getModuleInfo () : IModuleInfo {
               // return the module info
               return _moduleInfo;
        }
        public function loadModule () : void {        	
            // load the module
        	getModuleInfo().load();
        }
        public function unloadModule () : void {
        	// unload the module
        	getModuleInfo().release();

        }
        protected function onModuleProgress (e:ModuleEvent) : void {
                trace ("ModuleEvent.PROGRESS received: " + e.bytesLoaded + " of " + e.bytesTotal + " loaded.");
        }
        private function onModuleSetup (e:ModuleEvent) : void {
                trace ("ModuleEvent.SETUP received");
                // cast the currentTarget
                var moduleInfo:IModuleInfo = e.currentTarget as IModuleInfo;
                trace ("Calling IModuleInfo.factory.info ()");
                // grab the info and display information about it
                var info:Object = moduleInfo.factory.info();
                for (var each:String in info) {
                    trace ("     " + each + " = " + info[each]);
                }
                
       }
       private function onModuleReady (e:ModuleEvent):void { 
                trace ("ModuleEvent.READY received");
                // cast the currentTarget
                var moduleInfo:IModuleInfo = e.currentTarget as IModuleInfo;
                // Add an instance of the module's class to the 
                // display list. 
                trace ("Calling IModuleInfo.factory.create ()");
                armyModule = moduleInfo.factory.create () as IArmyModule;
                armyModule.initialize2();
               	var evt:AlarmEvent = new AlarmEvent(armyModule);
               	_moduleInfo.removeEventListener(ModuleEvent.READY, onModuleReady);
	           _moduleInfo.removeEventListener(ModuleEvent.SETUP, onModuleSetup);
	           _moduleInfo.removeEventListener(ModuleEvent.UNLOAD, onModuleUnload);
	           _moduleInfo.removeEventListener(ModuleEvent.PROGRESS, onModuleProgress);
           
               	moduleLoaded = true;
                dispatchEvent(evt);
                //moduleLoaded = true;
       }
       public function onModuleUnload (e:ModuleEvent) : void {
       		trace ("ModuleEvent.UNLOAD received");                
       } 
            

	}
}